// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "CaeRequestParams.h"
#include <ctime>
#include <utility>
#include "CaeCommon.h"
#include "CaeLogConfig.h"

using namespace std;

CaeRequestParams::CaeRequestParams()
{
    m_useUrl = false;
}

CaeRequestParams::~CaeRequestParams() = default;

CaeRequestParams::CaeRequestParams(string method, string url, string queryParams, string payload, bool useUrl)
{
    m_useUrl = useUrl;
    m_method = method;
    m_url = url;
    m_queryParams = queryParams;
    m_payload = payload;
}

bool CaeRequestParams::GetUseUrl()
{
    return m_useUrl;
}

const string CaeRequestParams::GetMethod()
{
    return m_method;
}

const string CaeRequestParams::GetHost()
{
    return m_host;
}

const string CaeRequestParams::GetUri()
{
    return m_uri;
}

const string CaeRequestParams::GetUrl()
{
    return m_url;
}

const string CaeRequestParams::GetQueryParams()
{
    return m_queryParams;
}

const string CaeRequestParams::GetPayload()
{
    return m_payload;
}

const std::set<Header> *CaeRequestParams::GetHeaders()
{
    return &m_headers;
}

void CaeRequestParams::AddHeader(string key, string value)
{
    m_headers.insert(Header(key, value));
}

std::string CaeRequestParams::InitHeaders(time_t validDateTime)
{
    if (0 == validDateTime || validDateTime < time(nullptr)) {
        LOGE("Valid date time isn't valid");
        return "";
    }
    std::string validTime = CaeCommon::To24Hour(validDateTime);
    m_headers.insert(Header("valid_date_time", validTime));
    // avoid intercepted by nginx
    m_headers.insert(Header("valid-date-time", validTime));
    return validTime;
}